I want to share a take on grammar-constrained generation I've been working on for a while. This post is a preliminary writeup: enough detail to communicate the core read-GLR-stack-with-a-weighted-automata idea (and timestamp it). I’ll likely follow up with a post on the various tricks you need to actually build the damn things without exploding.
You have a grammar (often a JSON Schema) and you want the LLM to produce grammatically valid output.
At each step, the LLM outputs logits over the vocabulary . You sample a token. Append it to the output. Repeat.
Constrained decoding is: don’t sample tokens that would make the output grammatically invalid.
So we maintain a constraint state and, for each step, compute a mask of allowed tokens, and apply it to the logits.
No more broken JSON.
Think of constrained decoding as two functions:
get_mask(state) returns a bitmask over the LLM vocabulary.commit(state, token) updates the parser state with the chosen token.At runtime the loop looks like:
loop:
parallel:
GPU: logits
CPU: mask
apply mask
sample token
commit token to model and constraint
LLM inference is batched: multiple user requests run on the GPU at once. And mask generation sits directly on the critical decoding path.
If you want to generate 1k tokens per second, you have 1 millisecond to produce a mask. And if you miss even a single one - if a mask takes 2ms instead of 1 - it screws up scheduling; the GPU sits waiting on the straggler mask, and suddenly your p99 latency is trash.
Masks can't be late; they have to arrive on time every time. It's worst-case performance that matters.
It’s not “can you do masking fast on average?” It’s “can you do it fast every single step?”
The commit op is analogous to incremental parsing: you’re incrementally parsing a growing prefix.
Many battle-tested incremental parsing systems like tree-sitter and Lezer use Generalized LR (GLR) parsing with a graph-structured stack (GSS).
Glossary
--.Now you have a parse state (often a GSS), and you want a mask over LLM tokens.
A straightforward approach is:
For each LLM token , check whether appending it keeps the parse valid.
But you don’t append an LLM token to the parser. You append terminal symbols.
And a single LLM token is a byte string which might:
"}\n" could lex as RBRACE NEWLINE),true),So “try token ” is really “try every way ’s bytes could lex into some terminal sequence, and then try advancing the parser for that terminal sequence.”
A common direction is:
This can work well for many grammars and workloads. People implement this and get decent average performance, especially for “simple” grammars.
But for general CFG-ish constraints (especially when you care about worst-case), you run into two issues:
Even if your lexical side is deterministic (say you enforce longest-match, or you have a DFA lexer), a single grammar terminal shift can trigger a chain of reductions.
In an LR parser, “shift terminal ” is often really:
Those reduction chains are table-driven and fast in the happy path, but in GLR they can fan out. The operations are pointer-heavy and cache-unfriendly: you’re manipulating a GSS, merging nodes, pruning branches, etc.
If you do that inside masking—i.e. while traversing a trie of possible tokens—you’re effectively building and mutating a GSS for a hypothetical next token, and doing it potentially thousands of times per step.
That’s exactly the kind of work you don’t want on the critical path.
Even with tries, there are some tokens whose byte strings correspond to exceedingly long sequences of terminals. Sometimes these get quickly pruned, e.g. ????. But not always.
Disambiguation
MINUS, NUMBER, IDENT. This is what your parser consumes.For example, Python will happily accept monstrosities like:
The sixteen hyphens get tokenised (BPE) as a single LLM token:
But token 7535 lexes to 16 MINUS terminals:
Every time a mask is generated, the parser must ingest all 16 MINUS terminals of token 7535. This forces it to do a lot of terminal-level processing—16 shifts through the grammar, each of which may involve many reductions—triggered by a single LLM token.
You can transform the grammar to remove unit/null reductions. You can aggressively merge and simplify GSS nodes. You can add clever memoization, early exits. But in my experience, these pesky LLM tokens with complicated lexes are just really hard to engineer away.
I tried hard to make the “trie + incremental parse simulation” approach behave well in worst-case latency terms. In my experience, it’s a dead end if you’re aiming for predictable sub-millisecond masking on arbitrary inputs/grammars.
Token validity depends on what's on the stack. Instead of asking "does LLM token t work on this stack?" for each token, ask "given this stack, which LLM tokens work?"
That's the reframing. Now we need a data structure that turns "stack → allowed tokens" into something we can execute quickly.
That’s where the weighted automaton comes in.
Think of a finite automaton, except each transition carries a weight, and traversing paths accumulates weights.
In our weighted automata:
The automaton reads (a representation of) your current parse configuration:
A token is valid if it’s valid on any parse path in the GSS, so on a GSS you just union the results across paths.
Another way to see it (which helped me):
That set of stacks is a regular language over parser state IDs (this is closely related to the classical “viable prefixes are regular” result from LR theory).
So you can imagine an automaton that recognizes “stacks on which token is valid.”
Of course, if you do that for every , you’d have 200k automata. That’s useless at runtime.
A weighted automaton is how you smash those 200k membership tests into one run:
Same computation, but 'vectorized' across tokens via bitsets (although I find rangesets perform better than bit vectors, so maybe 'vectorized' isn't quite accurate).
In practice you rarely need the full stack. As you scan states, each token’s fate becomes fixed: it either already reaches an accepting state (so it will stay valid) or it has been filtered out forever. Once a token is decided, pushing it deeper won’t change anything.
So maintain a “decided” set as you go. Peel those tokens off the frontier. When everything left is decided, you can stop immediately—no need to read the rest of the stack.
For a single LR stack, the runtime looks like:
def get_mask(stack_state_ids_top_to_bottom):
# frontier: map automaton_state -> bitset(tokens)
frontier = { A.start: ALL_TOKENS }
for sid in stack_state_ids_top_to_bottom:
new = {}
for a_state, tokens in frontier.items():
for (a2, weight) in A.step(a_state, sid):
tokens2 = tokens & weight # ∩ (filter)
if tokens2.any():
new[a2] = new.get(a2, EMPTY) | tokens2 # ∪ (merge)
frontier = new
if decided(frontier):
break
return combine_accepting(frontier)
This is the key operational point:
For a GSS, you do basically the same computation, but over a graph rather than a single list. Conceptually:
A token is valid if it’s valid on any stack path, so whenever GSS paths merge you union their token sets, and whenever automaton paths merge you union there too.
The important part is: it’s still the same “bitset flows through a graph via ∩ and ∪” pattern. No backtracking, no “try token ” loop.
For a GSS, you do basically the same computation but over a graph rather than a single list. (Details omitted here, but the point is: it’s the same ∩/∪ propagation pattern.)
Mask computation becomes:
Critically:
Work is proportional to stack structure, not the grammar complexity or vocabulary size.
commit(state, token) uses GLR machinery:
get_mask(state) uses the weighted automaton:
So you get a split where:
commit is responsible for advancing the parser state with a chosen LLM tokenget_mask is responsible for cheaply answering “what LLM tokens could be next?”I’m avoiding “this is optimal” as a theorem. (Blah blah blah I mean GLR doesn't really have 'this is optimal' theorems, but it feels optimal. Maybe just because so many people use it. Maybe it's just because it's fast. Maybe there's some deeper reason. IDK. But it does feel optimal.)
On the commit side, GLR-style techniques are already a strong fit for incremental CFG parsing. Table-driven decisions, compact ambiguity representation via GSS. There's a reason people working on these kinds of problems keep converging on GLR.
On the mask side, the weighted automaton performs the minimum work you'd reasonably hope for: read the stack once, never backtrack, stop when determined. Inner loops are bitset intersections and unions—fast, predictable.
This doesn’t magically resolve the lack of strict bounds on GLR parser time complexity, behaviour in pathological cases. You still inherit those. (Which are?? Theorems? Lack of theorems proving efficiency?) But... blah blah.
Precompiling the grammar into a weighted automaton involves determinization and minimization in a semiring setting. Large grammars can blow up if you're careless.
Getting compile-time and memory to behave took most of my engineering effort. That's probably the next post.